﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHControlLib;
using thinger.MTHModels;

namespace thinger.MTHProject
{
    public partial class FrmMonitor : Form
    {
        public FrmMonitor()
        {
            InitializeComponent();

            //设置日志的表格的宽度
            this.lvInto.Columns[1].Width = this.lvInto.Width - this.lvInto.Columns[0].Width - 25;

            //设施曲线的设置
            SetChart();

            //定时器更新数据
            Timer timer = new Timer();
            //定时器时间
            timer.Interval = 1000;

            //定时器的事件
            timer.Tick += Timer_Tick;

            //定时器开始
            timer.Start();

            //窗体关闭时关闭定时器
            this.FormClosing += (sender, e) =>
            {
                timer.Stop();
            };

            


        }


        private void Timer_Tick(object sender, EventArgs e)
        {
            Device device = CommonMethods.Device;
            if (device != null && device.IsConnected)
            {
                UpData(CommonMethods.Device);
            }
           
            
        }


        //加载数据
        private void UpData(Device device)
        {

            foreach (var item in this.panelEnhanced1.Controls.OfType<Dashboard>())
            {
                //更新温度
                if (device.DictionaryVarValue.ContainsKey(item.TempVarName.ToString().Trim()))
                {
                    if (device[item.TempVarName.ToString().Trim()] != null)
                    {
                        item.TempValue = (float)device[item.TempVarName.ToString().Trim()];
                    }

                }

                //更新湿度
                if (device.DictionaryVarValue.ContainsKey(item.HumidityVarName.ToString().Trim()))
                {
                    if (device[item.HumidityVarName.ToString().Trim()] != null)
                    {
                        item.HumidityValue = (float)device[item.HumidityVarName.ToString().Trim()];
                    }

                }
                //更新状态
                if (device.DictionaryVarValue.ContainsKey(item.StatusVarName.ToString().Trim()))
                {
                    if (device[item.StatusVarName.ToString().Trim()] != null)
                    {
                        item.ModbusError = (bool)device[item.StatusVarName.ToString().Trim()];
                    }

                }

                //更新实施趋势
                List<double> chartData = new List<double>();

                for (int i = 1; i <= 6; i++)
                {
                    chartData.Add(Convert.ToDouble(device[$"模块{i}温度"]));
                    chartData.Add(Convert.ToDouble(device[$"模块{i}湿度"]));
                }
                //ChartTH加入数据
                this.ChartTH.PlotSingle(chartData);
            }
        }

        //实施趋势的配置设置
        private void SetChart()
        {
            //设置X轴的数据类型以及格式
            this.ChartTH.XDataType = SeeSharpTools.JY.GUI.StripChartX.XAxisDataType.TimeStamp;
            this.ChartTH.TimeStampFormat = "HH:mm:ss";
            //设置图例
            this.ChartTH.LegendVisible = true;
            //设置显示数据点
            this.ChartTH.DisplayPoints = 4000;
            //Y轴范围
            this.ChartTH.AxisX.Minimum = 0.0f;
            this.ChartTH.AxisX.Maximum = 100.0f;
            this.ChartTH.AxisX.AutoScale = false;//设置x轴成非自适应
            //清除原来的曲线
            this.ChartTH.Series.Clear();//将原来的曲线清除
            //设置曲线的数量
            this.ChartTH.SeriesCount = 12;

            //设置具体的曲线
            for (int i = 0; i < 12; i++)
            {
                //设置曲线的名称
                this.ChartTH.Series[i].Name = i % 2 == 0 ? $"{i / 2 + 1}#站点温度" : $"{i / 2 + 1}#站点湿度";

                //设置曲线不可见
                this.ChartTH.Series[i].Visible = false;

                //设置曲线的粗细
                this.ChartTH.Series[i].Width = SeeSharpTools.JY.GUI.StripChartXSeries.LineWidth.Middle;

                //设置曲线的Y轴
                this.ChartTH.Series[i].YPlotAxis = SeeSharpTools.JY.GUI.StripChartXAxis.PlotAxis.Primary;
            }

            //1#站点温湿度默认显示
            this.chkTemp1.Checked = true;
            this.chkHum1.Checked = true;
        }

        public string CunrrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }


        /// <summary>
        /// 日志的方法
        /// </summary>
        /// <param name="level"></param>
        /// <param name="content"></param>
        public void AddLog(int level, string content)
        {
            if (level < 0)
            {
                level = 0;
            }
            if (level > 2)
            {
                level = 2;
            }
            //判断是不是其他线程，需不需要调用Invoke
            if (this.lvInto.InvokeRequired)
            {
                //其他线程调用控件
                this.Invoke(new Action<int, string>(AddLog), level, content);
            }
            else
            {
                ListViewItem listViewItem = new ListViewItem(CunrrentTime, level);
                listViewItem.SubItems.Add(content);
                this.lvInto.Items.Add(listViewItem);
                //将最新的日志放在最下面
                this.lvInto.Items[this.lvInto.Items.Count - 1].EnsureVisible();
            }
        }
        /// <summary>
        /// 修改checked时触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkCommon_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is CheckBoxEx checkBoxEx)
            {
                if (checkBoxEx.Tag != null && checkBoxEx.Tag.ToString().Length > 0) 
                {
                    this.ChartTH.Series[Convert.ToInt32(checkBoxEx.Tag.ToString())].Visible = checkBoxEx.Checked;
                }
            }
        }
    }
}
